package com.yang;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.ArrayList;
import java.util.List;

public final class YangShortcut extends JavaPlugin implements Listener {

    private static FileConfiguration config = null;

    @Override
    public void onEnable() {
        this.saveDefaultConfig();
        config = getConfig();
        getServer().getPluginManager().registerEvents(this, this);
        Bukkit.getPluginCommand("yangShortcut").setExecutor(this);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        Player player = ((Player) sender).getPlayer();
        if (!label.equalsIgnoreCase("yangShortcut")) {
            player.sendMessage(ChatColor.YELLOW + "指令不太对哦~");
            return true;
        }
        if (!(sender instanceof Player)) {
            sender.sendMessage(ChatColor.YELLOW + "你不是玩家哦~");
            return true;
        }
        String item = config.getString("yang.clock.item");
        ItemStack itemStack = new ItemStack(Material.matchMaterial(item));
        ItemMeta itemMeta = itemStack.getItemMeta();
        List<String> loreList = new ArrayList<>();
        loreList.add("快捷菜单");
        itemMeta.setLore(loreList);
        itemMeta.setDisplayName(ChatColor.GREEN + "快捷菜单");
        itemStack.setItemMeta(itemMeta);
        player.getInventory().addItem(itemStack);
        return true;
    }

    @EventHandler
    public void PlayerInteractEvent(PlayerInteractEvent playerInteractEvent) {
        EquipmentSlot hand = playerInteractEvent.getHand();
        if (hand == EquipmentSlot.HAND) {
            ItemStack itemStack = playerInteractEvent.getPlayer().getInventory().getItemInMainHand();
            String item = config.getString("yang.clock.item");
            if (itemStack.getType() == Material.matchMaterial(item)) {
                String command = config.getString("yang.clock.command");
                playerInteractEvent.getPlayer().performCommand(command);
            }
        }
    }

    @EventHandler
    public void PlayerSwapHandItemsEvent(PlayerSwapHandItemsEvent playerSwapHandItemsEvent) {
        Player player = playerSwapHandItemsEvent.getPlayer();
        if (player.isSneaking()) {
            String command = config.getString("yang.shortcut.command");
            player.performCommand(command);
        }
        playerSwapHandItemsEvent.setCancelled(true);

    }


    @Override
    public void onDisable() {
    }
}
